﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.Security;
using SubSonic;

public partial class User_Employer_SendSMS : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        LinkButtonDelete.Attributes.Add("onclick", "return confirm('هشدار : آيا از حذف فرد مطمئن هستيد؟');");
    }
    protected void PopupControlExtender1_Dispose(object sender, EventArgs e)
    {


        PopupControlExtender1.Commit("notificIds");


    }
    protected void ButtonSelectUser_Click(object sender, EventArgs e)
    {

    }
    protected void LinkButtonLoad_Click(object sender, EventArgs e)
    {
        loadlist();
    }
    protected void LinkButtonDelete_Click(object sender, EventArgs e)
    {
        if (ListBoxUsers.Items.Count == 0)
        {
            lblStatus.Text = ".لیست خالی میباشد";
            lblStatus.CssClass = "error";
        }
        if (ListBoxUsers.Items.Count > 0)
        {
            if (ListBoxUsers.SelectedIndex >= 0)
            {
                List<Guid> list = new List<Guid>();
                list = Session["notificIds"] as List<Guid>;
                list.RemoveAt(ListBoxUsers.SelectedIndex);
                
                Session["notificIds"] = list;
                loadlist();
            }
            else
            {
                lblStatus.Text = ".لطفا ابتدا فرد را انتخاب بفرمایید ";
                lblStatus.CssClass = "error";
            }
        }
    }
    public void loadlist()
    {
        List<Guid> list = new List<Guid>();
        if (Session["notificIds"] != null)
        {
            ListBoxUsers.Items.Clear();
            list = Session["notificIds"] as List<Guid>;
            for (int i = 0; i < list.Count; i++)
            {
                ListItem l = new ListItem();
                Business.ReturnUserDetail b = new Business.ReturnUserDetail();
                MembershipUser u = Membership.GetUser(new Guid(list[i].ToString()));
                l.Text = b.ReturnName(u.UserName);
                l.Value = list[i].ToString();
                ListBoxUsers.Items.Add(l);
                ListBoxUsers.DataBind();
            }
        }
        else
        {
            lblStatus.CssClass = "error";
            lblStatus.Text = "بارگذاری با خطا مواجه شد. هیچ فردی انتخاب نشده است.";
        }
    }
    protected void ButtonSend_Click(object sender, EventArgs e)
    {
        Business.Notification n = new Business.Notification();
        if (CheckBoxNotific.Checked)
        {
            if(ListBoxUsers.Items.Count>0)
            {
                List<Guid> g = new List<Guid>();
                for (int i = 0; ListBoxUsers.Items.Count > i; i++)
                {
                    g.Add(new Guid( ListBoxUsers.Items[i].Value));
                }
                saveinnernotific(g);
            }
            if (CheckBoxAdvisorHaghighi.Checked)
            {
                List<Guid> s = new List<Guid>();
                s = n.FindUserIdsByRole(1);
                saveinnernotific(s);

            }
            if (CheckBoxAdvisorHoghooghi.Checked)
            {
                List<Guid> s = new List<Guid>();
                s = n.FindUserIdsByRole(0);
                saveinnernotific(s);
            }
            if (CheckBoxSurveyorHaghighi.Checked)
            {
                List<Guid> s = new List<Guid>();
                s = n.FindUserIdsByRole(3);
                saveinnernotific(s);
            }
            if (CheckBoxSurveyorHoghooghi.Checked)
            {
                List<Guid> s = new List<Guid>();
                s = n.FindUserIdsByRole(2);
                saveinnernotific(s);
            }
            if (CheckBoxFourthFactor.Checked)
            {
                List<Guid> s = new List<Guid>();
                s = n.FindUserIdsByRole(4);
                saveinnernotific(s);
            }
            if (CheckBoxEmployees.Checked)
            {
                List<Guid> s = new List<Guid>();
                s = n.FindUserIdsByRole(5);
                saveinnernotific(s);
                       }
        }
        if (CheckBoxSMS.Checked)
        {
            if (ListBoxUsers.Items.Count > 0)
            {
                List<Guid> g = new List<Guid>();
                for (int i = 0; ListBoxUsers.Items.Count > i; i++)
                {
                    g.Add(new Guid(ListBoxUsers.Items[i].Value));
                    
                }
               string[] m= n.FindMobile(g);
               SendSmS(m);
            }
            if (CheckBoxAdvisorHaghighi.Checked)
            {
                string[] m= n.FindMobilesByRole(1);
                SendSmS(m);
            }
            if (CheckBoxAdvisorHoghooghi.Checked)
            {
                string[] m = n.FindMobilesByRole(0);
                SendSmS(m);
            }
            if (CheckBoxSurveyorHaghighi.Checked)
            {
                string[] m = n.FindMobilesByRole(3);
                SendSmS(m);
            }
            if (CheckBoxSurveyorHoghooghi.Checked)
            {
                string[] m = n.FindMobilesByRole(2);
                SendSmS(m);
            }
            if (CheckBoxFourthFactor.Checked)
            {
                string[] m = n.FindMobilesByRole(4);
                SendSmS(m);
            }
            if (CheckBoxEmployees.Checked)
            {
                string[] m = n.FindMobilesByRole(5);
                SendSmS(m);
            }
        }
    }
    public void SendSmS(string[] D)
    {
        try
        {
            com.sobhsms.www.Service1 s = new com.sobhsms.www.Service1();

            string str = TextBoxText.Text;
            str = str.Replace("$", Environment.NewLine);

            if (D.Length > 0)
            {
                long[] r = s.SendPackageMessageConcFast(D, str, "Pwebsrvbnydmskn90");
            }
            //Response.Write(r[0].ToString());
            lblStatus.CssClass = "success";
            lblStatus.Text = "ارسال پیغام با موفقیت انجام شد.";
        }
        catch
        {

            lblStatus.CssClass = "error";
            lblStatus.Text = "ارسال پیام کوتاه(SMS) با خطا مواجه شد.اتصال اینترنت خود را چک کنید";
        }
    }
    public void saveinnernotific(List<Guid> userids)
    {
        try
        {

            for (int i = 0; i <= userids.Count - 1; i++)
            {
                DataAccess.TblNotification n = new DataAccess.TblNotification();
                n.DateX = DateTime.Now;
                string str = TextBoxText.Text;
                str = str.Replace("$", Environment.NewLine);
                n.Message = str;
                n.UserId = new Guid(userids[i].ToString());
                n.IsRead = false;
                n.Save();
            }
            lblStatus.CssClass = "success";
            lblStatus.Text = "ارسال پیغام با موفقیت انجام شد.";
        }
        catch 
        {

            lblStatus.CssClass = "error";
            lblStatus.Text = "ارسال پیغام درون سیستمی با خطا مواجه شد.";
        }
    }
}